package per.cl.nacos;

import com.alibaba.nacos.api.annotation.NacosInjected;
import com.alibaba.nacos.api.config.annotation.NacosValue;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.spring.context.annotation.config.NacosPropertySource;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author chenlei
 * @date 2021/5/17
 * @description
 **/
@SpringBootApplication
@RestController
@RefreshScope
@EnableDiscoveryClient
@EnableDubbo
public class NacosApplication {

    public static void main(String[] args) {
        SpringApplication.run(NacosApplication.class);
    }

    @Value(value = "${property:hello}")
    private String property;

    @NacosInjected
    private NamingService namingService;

    @GetMapping("/hello")
    public String hello(){
        return property;
    }

    @GetMapping("/register")
    public String register() throws NacosException {
        namingService.registerInstance("demo", "127.0.0.1", 8080);
        return "success";
    }

    @GetMapping("/deRegister")
    public String deRegister() throws NacosException {
        namingService.deregisterInstance("demo", "127.0.0.1", 8080);
        return "success";
    }
}
